﻿using System;
using Ledict.Utility.Model;

namespace Ledict.Utility.Common
{
    /// <summary>
    /// 调度自定义异常处理类
    /// </summary>
    public class ExceptionDo
    {
        /// <summary>
        /// 异常转换为通俗提示信息
        /// 注意：提示信息与所列举异常类型不一定完全对应；
        ///       如有所需可在后面按照格式添加新的异常类型
        /// </summary>
        /// <param name="e">异常基类</param>
        /// <returns></returns>
        /// <remarks>
        /// Create Date:2014-08-11
        /// Create Author:Zhangpeng
        /// </remarks>
        public static JsonMsg ExceptionToErrorInfo(Exception e)
        {
            var jsonMsg = new JsonMsg { Result = false };
            if (e is TimeoutException)
            {
                jsonMsg.Message = "连接超时，请检查网络";
                return jsonMsg;
            }
            if (e is FormatException)
            {
                jsonMsg.Message = "发生数据转换异常，请检查";
                return jsonMsg;
            }

            if (e is ArgumentOutOfRangeException)
            {
                jsonMsg.Message = "超出索引范围，请检查";
                return jsonMsg;
            }
            jsonMsg.Message = "未知原因引起系统异常，请联系管理员";
            return jsonMsg;
        }
    }
}
